<template>
  <view class="app-container">
    <AdaptiveNavBar
      title="行程"
      :showBack="true"
      :showHome="true"
      buttonStyle="separate"
      backgroundColor="#ffffff"
      textColor="#1A1A1A"
      :showShadow="false"
    >
    </AdaptiveNavBar>

    <view class="dynamics-page" :style="{ marginTop: $navbarUtil.getNavbarTotalHeightRpx() }">
      <view class="search-filter-section">
        <view class="filter-select" @tap="toggleCategory">
          <text class="filter-select-text">{{ selectedCategoryName }}</text>
          <FontIcon name="icon-zhankai_xiala_xiajiantou" :size="18" color="#000000"></FontIcon>
        </view>
        <view class="search-wrapper">
          <FontIcon name="icon-search" :size="24" color="#000000"></FontIcon>
          <input class="search-input" v-model="keyword" placeholder="输入搜索的关键词"
            placeholder-style="color:#B2B2B2" confirm-type="search" @confirm="handleSearch" />
        </view>
      </view>

      <view class="tabs">
        <view v-for="tab in tabs" :key="tab.value" class="tab-item"
          :class="{ active: currentTab === tab.value }" @tap="changeTab(tab.value)">
          {{ tab.label }}
          <view class="tab-indicator" v-if="currentTab === tab.value"></view>
        </view>
      </view>

      <view class="dynamics-list" v-if="!isLoading && dynamics.length > 0">
        <view class="dynamic-item" v-for="dynamic in dynamics" :key="dynamic.id">
          <view class="dynamic-header">
            <image :src="dynamic.avatar" class="user-avatar" mode="aspectFill"></image>
            <view class="user-info">
              <text class="username">{{ dynamic.username }}</text>
              <text class="post-time">{{ dynamic.time }}</text>
            </view>
          </view>
          <view class="dynamic-content">
            <text class="content-text">{{ dynamic.content }}</text>
            <image v-if="dynamic.image" :src="dynamic.image" class="content-image" mode="aspectFill"></image>
          </view>
          <view class="dynamic-footer">
            <view class="footer-item" @click="handleLike(dynamic.id)">
              <FontIcon name="icon-dianzan" :size="28" :color="dynamic.liked ? '#ff4757' : '#999'"></FontIcon>
              <text :style="{ color: dynamic.liked ? '#ff4757' : '#999' }">
                {{ dynamic.likes }}
              </text>
            </view>
            <view class="footer-item" @click="handleComment(dynamic.id)">
              <FontIcon name="icon-pinglun" :size="28" color="#999"></FontIcon>
              <text>{{ dynamic.comments }}</text>
            </view>
            <view class="footer-item" @click="handleShare(dynamic.id)">
              <FontIcon name="icon-fenxiang" :size="28" color="#999"></FontIcon>
              <text>分享</text>
            </view>
          </view>
        </view>
      </view>

      <view class="empty-state" v-else-if="!isLoading">
        <FontIcon name="icon-kongzhuangtai" :size="120" color="#CCCCCC"></FontIcon>
        <text class="empty-text">暂无动态内容</text>
      </view>

      <view class="loading-state" v-if="isLoading">
        <view class="custom-loading"></view>
        <text class="loading-text">加载中...</text>
      </view>
    </view>
    <CustomTabBar></CustomTabBar>
  </view>
</template>

<script>
import { updateTabBarSelected } from '@/utils/tabBarUtil';
import AdaptiveNavBar from '@/components/AdaptiveNavBar.vue';
import CustomTabBar from '@/components/CustomTabBar.vue';

export default {
  components: {
    AdaptiveNavBar,
    CustomTabBar
  },
  data() {
    return {
      keyword: '',
      cateId: '',
      selectedCategoryName: '',
      isLoading: false,
      dynamics: [],
      tabs: [
        { value: 'all', label: '全部' },
        { value: 'signup', label: '报名中' },
        { value: 'ongoing', label: '行程中' },
        { value: 'finished', label: '已结束' }
      ],
      currentTab: 'signup',
      categoryList: []
    }
  },
  onLoad() {},
  onShow() {
    try {
      uni.hideTabBar({ animation: false });
    } catch (e) {}
    updateTabBarSelected.call(this, 2);
    this.loadDynamicsData();
  },
  onPullDownRefresh() {
    this.loadDynamicsData().finally(() => {
      uni.stopPullDownRefresh();
    });
  },
  methods: {
    handleBack() {
      uni.navigateBack();
    },
    handleHome() {
      try {
        uni.switchTab({ url: '/pages/customer/index/index' });
      } catch (e) {
        uni.navigateTo({ url: '/pages/customer/index/index' });
      }
    },
    handleRecord() {
      uni.showToast({ title: '记录功能开发中', icon: 'none' });
    },
    handleMore() {
      uni.showActionSheet({
        itemList: ['刷新', '分享'],
        success: () => {},
        fail: () => {}
      });
    },
    toggleCategory() {
      uni.showToast({ title: '筛选功能开发中', icon: 'none' });
    },
    changeTab(value) {
      if (this.currentTab !== value) {
        this.currentTab = value;
        this.loadDynamicsData();
      }
    },
    handleSearch() {
      if (this.keyword.trim() || this.cateId) {
        this.loadDynamicsData();
      } else {
        uni.showToast({ title: '请输入搜索关键词', icon: 'none' });
      }
    },
    async loadDynamicsData() {
      try {
        this.isLoading = true;
        // TODO: 调用真实接口获取动态数据
        // const res = await request({
        //   url: '/dynamics/list',
        //   method: 'GET',
        //   data: {
        //     tab: this.currentTab,
        //     keyword: this.keyword,
        //     cateId: this.cateId
        //   }
        // });
        // if (res.code === 200) {
        //   this.dynamics = res.data || [];
        // }
        this.dynamics = [];
      } catch (error) {
        console.error('加载动态失败:', error);
        uni.showToast({ title: '加载失败，请重试', icon: 'none' });
      } finally {
        this.isLoading = false;
      }
    },
    handleLike(id) {
      const dynamic = this.dynamics.find(item => item.id === id);
      if (dynamic) {
        dynamic.liked = !dynamic.liked;
        dynamic.likes += dynamic.liked ? 1 : -1;
      }
    },
    handleComment() {
      uni.showToast({ title: '评论功能开发中', icon: 'none' });
    },
    handleShare() {
      uni.showToast({ title: '分享功能开发中', icon: 'none' });
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  min-height: 100vh;
  background-color: #FAFAFA;
  display: flex;
  flex-direction: column;
}

.navbar-right-actions {
  display: flex;
  align-items: center;
  gap: 12rpx;
}

.header-btn {
  width: 72rpx;
  height: 72rpx;
  border-radius: 36rpx;
  background-color: #F5F5F5;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dynamics-page {
  flex: 1;
  // padding: 24rpx 0rpx;
  padding-bottom: calc(160rpx + env(safe-area-inset-bottom));
}

.search-filter-section {
  display: flex;
  align-items: center;
  gap: 16rpx;
  // margin-bottom: 16rpx;
  padding:24rpx;
  background: #ffffff;
}

.filter-select {
  display: flex;
  align-items: center;
  gap: 8rpx;
  padding: 0 24rpx;
  height: 60rpx;
  border-radius: 30rpx;
  // background-color: #F5F5F5;
  // font-size: 26rpx;
  color: #111111;
}

.filter-select-text {
  margin-right: 8rpx;
}

.search-wrapper {
  flex: 1;
  height: 60rpx;
  background-color: #F5F5F5;
  border-radius: 30rpx;
  display: flex;
  align-items: center;
  padding: 0 24rpx;
  gap: 12rpx;
}

.search-input {
  flex: 1;
  font-size: 26rpx;
  color: #333333;
}

.search-input::placeholder {
  color: #B2B2B2;
}

.tabs {
  display: flex;
  align-items: center;
  background-color: #ffffff;
  border-radius: 16rpx;
  overflow: hidden;
  margin-bottom: 24rpx;
}

.tab-item {
  flex: 1;
  text-align: center;
  padding: 24rpx 0;
  font-size: 28rpx;
  color: #666666;
  position: relative;
}

.tab-item.active {
  color: #F37B1D;
  font-weight: 600;
}

.tab-indicator {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 60%;
  height: 6rpx;
  border-radius: 3rpx;
  background: #F37B1D;
}

.dynamics-list {
  display: flex;
  flex-direction: column;
  gap: 16rpx;
}

.dynamic-item {
  background-color: #ffffff;
  border-radius: 16rpx;
  padding: 24rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
}

.dynamic-header {
  display: flex;
  align-items: center;
  margin-bottom: 16rpx;
}

.user-avatar {
  width: 72rpx;
  height: 72rpx;
  border-radius: 36rpx;
  margin-right: 16rpx;
  object-fit: cover;
  background-color: #f2f2f2;
}

.user-info {
  display: flex;
  flex-direction: column;
}

.username {
  font-size: 28rpx;
  font-weight: 600;
  color: #333333;
}

.post-time {
  font-size: 22rpx;
  color: #999999;
  margin-top: 4rpx;
}

.dynamic-content {
  margin-bottom: 20rpx;
}

.content-text {
  font-size: 28rpx;
  color: #444444;
  line-height: 1.6;
}

.content-image {
  width: 100%;
  height: 360rpx;
  border-radius: 12rpx;
  margin-top: 16rpx;
  object-fit: cover;
}

.dynamic-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-top: 1rpx solid #F0F0F0;
  padding-top: 16rpx;
}

.footer-item {
  display: flex;
  align-items: center;
  gap: 10rpx;
  font-size: 24rpx;
  color: #999999;
}

.empty-state,
.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 160rpx 0;
  color: #999999;
}

.custom-loading {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  border: 8rpx solid rgba(255, 122, 26, 0.15);
  border-top-color: #FF7A1A;
  animation: spin 1s linear infinite;
}

.loading-text,
.empty-text {
  margin-top: 24rpx;
  font-size: 28rpx;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
</style>